﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HuiZe.education.View.Ethan
{
    /// <summary>
    /// PART_Media_Photo.xaml 的交互逻辑
    /// </summary>
    public partial class PART_Media_Photo : UserControl
    {
        public PART_Media_Photo()
        {
            InitializeComponent();
        }

        public void SetPhoto(string _path)
        {
            if (string.IsNullOrEmpty(_path) || !File.Exists(_path))
            {
                PART_Photo.Source = null;
                return;
            }

            try
            {
                BitmapImage _bmp = new BitmapImage();
                _bmp.CacheOption = BitmapCacheOption.OnLoad;
                _bmp.BeginInit();
                _bmp.StreamSource = new MemoryStream(GetFileData(_path));
                _bmp.EndInit();
                _bmp.Freeze();

                PART_Photo.Source = _bmp;
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        public void Reset()
        {
            PART_Photo.Source = null;
        }

        byte[] GetFileData(string _path)
        {
            byte[] _buffers = null;
            using (var _fs = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                _buffers = new byte[_fs.Length];
                _fs.Read(_buffers, 0, _buffers.Length);
                _fs.Close();
            }
            return _buffers;
        }
    }
}
